package com.jandar.controller;

import com.jandar.service.AnalyzeService;
import com.jandar.service.BankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

@RestController
@RequestMapping("/analyzeAndStatisticsBank")
public class BankController {
    @Autowired
    BankService bankService;
    @Autowired
    AnalyzeService analyzeService;

//    @RequestMapping(path = "/download", method = RequestMethod.GET)
//    public ResponseEntity<InputStreamResource> downloadIncrementFilterResult(@RequestParam(name = "id") Integer id, @RequestParam(name = "type") String type) throws FileNotFoundException {
//        File excelFile = bankService.getFile(id, type);
//        if (excelFile != null && excelFile.exists()) {
//            return ResponseEntity.ok()
//                    .header("content-disposition", "attachment; filename=" + excelFile.getName())
//                    .contentLength(excelFile.length())
//                    .contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
//                    .body(new InputStreamResource(new FileInputStream(excelFile)));
//        } else {
//            return ResponseEntity.notFound().build();
//        }
//    }

//    @RequestMapping("upload/{bankName}")
//    public ResponseEntity uploadFile(MultipartFile file, @PathVariable("bankName") String bankName, HttpServletRequest request) {
//        bankService.uploadFile(file, bankName, request);
//        return ResponseEntity.ok("上传成功");
//    }

//    @RequestMapping("upload/{bankName}")
//    public ResponseEntity uploadFile(MultipartFile file, @PathVariable("bankName") String bankName, HttpServletRequest request) {
//        analyzeService.uploadFile(file,null,null,"",request);
//        return ResponseEntity.ok("上传成功");
//    }

//    @PostMapping("page")
//    public ResponseEntity page(@RequestBody BankService.PageParam pageParam, HttpServletRequest request) {
//        return ResponseEntity.ok(bankService.page(pageParam, request));
//    }
    @PostMapping("page")
    public ResponseEntity page(@RequestBody AnalyzeService.PageParam pageParam, HttpServletRequest request) {
        pageParam.setTypeName("银行");
        return ResponseEntity.ok(analyzeService.page(pageParam, request));
    }

    @PostMapping("pageBySheetName")
    public ResponseEntity pageBySheetName(@RequestBody BankService.PageParam pageParam) {
        return ResponseEntity.ok(bankService.pageBySheet(pageParam));
    }

    @GetMapping("listSheetName")
    public ResponseEntity listSheetName() {
        return ResponseEntity.ok(bankService.listSheetName());
    }

//    @GetMapping("listBankName")
//    public ResponseEntity listBankName() {
//        return ResponseEntity.ok(bankService.listBankName());
//    }

    @GetMapping("delete")
    public ResponseEntity delete(Integer id) {
        return ResponseEntity.ok(analyzeService.delete(id));
    }


//    @PostMapping("addBank")
//    public ResponseEntity addBank(@RequestBody BankService.SaveBankParam saveBankParam) {
//        return ResponseEntity.ok(bankService.saveBank(saveBankParam, 1));
//    }
//
//    @PostMapping("updateBank")
//    public ResponseEntity updateBank(@RequestBody BankService.SaveBankParam saveBankParam) {
//        return ResponseEntity.ok(bankService.saveBank(saveBankParam, 0));
//    }

    @GetMapping("deleteBank")
    public ResponseEntity deleteBank(String bankName) {
        return ResponseEntity.ok(bankService.deleteBank(bankName));
    }

    @GetMapping("listField")
    public ResponseEntity listField() {
        return ResponseEntity.ok(bankService.listField());
    }

    @GetMapping("listMappingByBank")
    public ResponseEntity listMappingByBank(String bankName) {
        return ResponseEntity.ok(bankService.listMappingByBank(bankName));
    }

    @PostMapping("pageBank")
    public ResponseEntity pageBank(@RequestBody BankService.PageParam param) {
        return ResponseEntity.ok(bankService.pageBank(param));
    }
//    @PostMapping("processBankDataApi")
//    public ResponseEntity processBankDataApi(@RequestBody ProcessBankDataApiParam param) {
//        analyzeAndStatisticService.processBankDataApi(param.data, param.mappingList);
//        return ResponseEntity.ok("success");
//    }
//
//    @Data
//    static class ProcessBankDataApiParam{
//        List<Map> data;
//        List<Map<String, Object>> mappingList;
//    }


}
